/** 
 * VariableAssignmentExpression.java
 * @author Lex
 * 2007-8-20 ����02:41:55
 */
package fun.parser.ast.node;

import fun.parser.EvaluationException;
import fun.parser.ast.node.base.BinaryExpression;
import fun.parser.ast.node.base.Expression;
import fun.parser.lexer.Token;
import fun.symtable.SymbolTable;
import fun.symtable.VariableEntry;

/**
 * ������ֵ
 */
public class VariableAssignmentExpression extends BinaryExpression {
	/**
	 * the firstExpression should be a VariableExpression
	 */
	public VariableAssignmentExpression(Token node, VariableExpression variable, Expression expression) {
		super(node, variable, expression);
	}

	public double getValue(SymbolTable symtable) throws EvaluationException {
		double value = this.getSecondExpression().getValue(null);
		VariableExpression var = (VariableExpression) getFirstExpression();
		String name = var.getVariableName();
		VariableEntry entry = symtable.getVariable(name);
		if (null == entry)
			entry = symtable.setVariable(name, value);
		entry.setValue(value);
		return value;
	}
}
